home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 696 < prev    next >
Text File  |  1996-08-06  |  4KB  |  124 lines

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.std.c
  4. Subject: Re: Restrictions on qsort compare function?
  5. Date: Thu, 04 Apr 96 18:57:54 GMT
  6. Organization: none
  7. Message-ID: <828644274snz@genesis.demon.co.uk>
  8. References: <4iokop$h4p@lyra.csx.cam.ac.uk> <4iqjar$2m9@usenet.pa.dec.com> <4jgltp$f9l@lyra.csx.cam.ac.uk>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4jgltp$f9l@lyra.csx.cam.ac.uk>
  15.            jkb@mrc-lmb.cam.ac.uk "James Bonfield" writes:
  16.  
  17. >I now agree that non antisymmetric or nontransitive sort comparison functions
  18. >are indeed invalid. I can see how this can be construed from the ansi
  19. >standard, but can also see how it's possible to construe otherwise.
  20.  
  21. I don't. 7.10.5.2:
  22.  
  23. "The contents of the array are sorted into ascending
  24.  order according to a comparison function pointed to by compar". 
  25.  
  26. If the comparison function produces inconsistent results then it is at odds
  27. with that sentence. At best that sentence has no well-defined meaning and
  28. the 'sort' operation as a whole has undefined behaviour.
  29.  
  30. >it doesn't really state such things explicitly. Anyway, that aside I thought I
  31. >should reply to the last note about this.
  32. >
  33. >In article <4iqjar$2m9@usenet.pa.dec.com> diamond@tbj.dec.com (Norman Diamond)
  34. > writes:
  35. >
  36. >>>such functions appear to make [one implementation's] qsort() function
  37. >>>underflow the passed array.
  38. >>
  39. >>This should not happen.
  40. >
  41. >Well, that depends on whether or not you class qsorts behaviour as undefined
  42. >for such functions.
  43.  
  44. To put it another way, if there is a defined behaviour with such a function,
  45. what is it?
  46.  
  47. >>>#define NUM_ELE 10
  48. >>>int main() {
  49. >>>    int i;
  50. >>>    int crashme;     /* removing this line fixes things! */
  51. >>
  52. >>This makes me suspicious that your test program is not exactly what you
  53. >>posted, and your test program has a bug in some other part of its code
  54. >>that already yielded undefined behavior.
  55. >
  56. >Actually it's true - it does cause it to go wrong. I've got an example now
  57. >that used explicitly defined numbers to cause a crash. With the crashme line
  58. >in the program dies. Without it the program modifies memory not within the
  59. >boundaries of qsort. My program is:
  60. >
  61. >#include <stdio.h>
  62. >#include <stdlib.h>
  63. >
  64. >static int sort_func(const void *pa, const void *pb)
  65. >{
  66. >    const int *a = (int *)pa;
  67. >    const int *b = (int *)pb;
  68.  
  69. It would be more consistent (as well as killing a warning from my compiler)
  70. if you cast to (const int *), better still don't cast at all.
  71.  
  72. >
  73. >    return *a > *b;
  74. >}
  75.  
  76. This clearly violates a 'shall' in the standard and results in undefined
  77. behaviour. With the specific data in this case return *a - *b; would be OK.
  78.  
  79. "The function shall return an integer less than, equal to, or greater than
  80.  zero if the first argument is considered to be respectively less than, equal
  81.  to, or greater than the second."
  82.  
  83. >#define NUM_ELE 10
  84. >int main() {
  85. >    int i;
  86. >    int crashme;     /* removing this line fixes things! */
  87. >    int sortme[NUM_ELE] = {148, 104, 126, 74, 108, 131, 129, 131, 125, 77};
  88. >
  89. >    for (i=0; i<NUM_ELE; i++) printf("%d ", sortme[i]);
  90. >    putchar('\n');
  91. >
  92. >    qsort((void *)(sortme+1), NUM_ELE-2, sizeof(int), sort_func);
  93. >
  94. >    for (i=0; i<NUM_ELE; i++) printf("%d ", sortme[i]);
  95. >    putchar('\n');
  96. >
  97. >    return 0;
  98. >}
  99. >
  100. >>b < c and c < a).  As for whether qsort() is required to contend with
  101. >>such nonsense, it "probably" isn't.
  102. >
  103. >Agreed. Hence the above discoveries are most likely entirely within the realm
  104. >of acceptability.
  105. >
  106. >Perhaps the most interesting point to come out of this is the inefficiency of
  107. >some qsort algorithms. Sorting 100,000 elements (with a "consistent" sort
  108. >comparison function) gave the following approximations (ran several times with
  109. >random input - of course these results may just reflect the random number
  110. >generator woes!):
  111. >
  112. >OSF/1 V3.0      ~72K
  113. >Linux (gnu lib) ~153K
  114. >Irix 5.3        ~170K
  115. >Solaris 5.3     ~305K
  116.  
  117. What do these numbers represent?
  118.  
  119. -- 
  120. -----------------------------------------
  121. Lawrence Kirby | fred@genesis.demon.co.uk
  122. Wilts, England | 70734.126@compuserve.com
  123. -----------------------------------------
  124.